package com.activenetwork.smlite.page.rfp.RfpMaintenanceView;

import java.text.ParseException;
import java.util.Calendar;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.activenetwork.custom.other.XCheckbox;
import com.activenetwork.custom.uielement.UIElement;
import com.activenetwork.custom.uielement.decorate.InJson;
import com.activenetwork.schema.model.rfp.RfpBasicType;
import com.activenetwork.smlite.page.rfp.RfpBasicsSubview;
import com.activenetwork.util.DateUtil;
import com.activenetwork.util.EnvContext;
import com.activenetwork.util.PageLoader;
import com.activenetwork.util.datepicker.DatePicker;
import com.activenetwork.util.dropdownmenu.DropdownMenu;

public class RfpSetting extends RfpBasicsSubview{
	private Logger logger = LoggerFactory.getLogger(RfpSetting.class);
	

	@InJson
	private UIElement rfpName;
	@InJson
	private UIElement dueDate;
	@InJson
	private UIElement needSleepingRoom;
	@InJson
	private UIElement needEventSpace;
	@InJson
	private UIElement needFoodBeverage;
	@InJson
	private UIElement precedingShoulder;
	@InJson
	private UIElement followingShoulder;
	
	/**
	 * Set rfp name
	 * @param basic
	 * @return
	 */
	public RfpSetting setRfpName(String rfpNameValue){
		rfpName.clear();
		rfpName.sendKeys(rfpNameValue);
		logger.debug("Set rfp name");
		return PageLoader.load(RfpSetting.class);
	}
	/**
	 * Set rfp due date
	 * @param basic
	 * @return
	 */
	public RfpSetting setDueDate(Calendar calendar){
		DatePicker dataPicker = new DatePicker(dueDate);
		dataPicker.pick(calendar);
		logger.debug("Set rfp due date");
		return PageLoader.load(RfpSetting.class);
	}
	/**
	 * Set event needs checkboxes
	 * @param basic
	 * @return
	 */
	public RfpSetting setEventNeeds(RfpBasicType basic){
		enableSleepingRoom(basic.getEventNeeds().isNeedSleepingRooms());
		enableEventSpace(basic.getEventNeeds().isNeedEventSpace());
		enableFoodBeverage(basic.getEventNeeds().isNeedFoodBeverage());
		return PageLoader.load(RfpSetting.class);
	}
	
	public void enableSleepingRoom(boolean toEnable){
		XCheckbox sleepingRoomCheck = new XCheckbox(needSleepingRoom);
		if(toEnable){
			sleepingRoomCheck.checkON();
			logger.debug("Set rfp need sleeping room - ON");
		}else{
			sleepingRoomCheck.checkOFF();
			logger.debug("Set rfp need sleeping room - OFF");
		}
	}
	
	public void enableEventSpace(boolean toEnable){
		XCheckbox eventSpaceCheck = new XCheckbox(needEventSpace);
		if(toEnable){
			eventSpaceCheck.checkON();
			logger.debug("Set rfp need event space - ON");
		}else{
			eventSpaceCheck.checkOFF();
			logger.debug("Set rfp need event space - OFF");
		}
	}
	
	public void enableFoodBeverage(boolean toEnable){
		XCheckbox foodBeverageCheck = new XCheckbox(needFoodBeverage);
		if(toEnable){
			foodBeverageCheck.checkON();
			logger.debug("Set rfp need food beverage - ON");
		}else{
			foodBeverageCheck.checkOFF();
			logger.debug("Set rfp need food beverage - OFF");
		}
	}
	
	
	/**
	 * Get the rfp start date from page
	 * @return
	 * @throws ParseException
	 */
	public Calendar getRfpStartDate() throws ParseException{
		WebDriver driver = EnvContext.webDriverTL.get();
		String dateStr = driver.findElement(By.cssSelector(".autotest-html-rfpbasicssubview label[data-smodel-path='minirfp.startDate']")).getText().trim();
		DateUtil du = new DateUtil("yyyy-MM-dd");
		return du.toCalendar(dateStr);
	}
	/**
	 * Get the rfp end date from page
	 * @return
	 * @throws ParseException
	 */
	public Calendar getRfpEndDate() throws ParseException{
		WebDriver driver = EnvContext.webDriverTL.get();
		String dateStr = driver.findElement(By.cssSelector(".autotest-html-rfpbasicssubview label[data-smodel-path='minirfp.endDate']")).getText().trim();
		DateUtil du = new DateUtil("yyyy-MM-dd");
		return du.toCalendar(dateStr);
	}
	/**
	 * Select preceding shoulder day dropdown
	 * @param precedingDay
	 * @return
	 */
	public RfpSetting setPrecedingShoulder(int precedingDay){
		DropdownMenu precedingShoulderDropdown = new DropdownMenu(precedingShoulder);
		precedingShoulderDropdown.select(String.valueOf(precedingDay));
		return this;
	}
	/**
	 * Select following shoulder day dropdown
	 * @param followingDay
	 * @return
	 */
	public RfpSetting setFollowingShoulder(int followingDay){
		DropdownMenu followingShoulderDropdown = new DropdownMenu(followingShoulder);
		followingShoulderDropdown.select(String.valueOf(followingDay));
		return this;
	}
}
